﻿using System;
using Box2DX.Collision;
using Box2DX.Common;
using Box2DX.Dynamics;

namespace Expression.Samples.Physics
{
	public static class BodyHelper
	{
		public static Body GetBodyForElement(Simulator simulator, Entity entity, BodyParameters bodyParameters)
		{
			BodyDef bodyDef = new BodyDef();
			bodyDef.Position = simulator.SimulatorContext.ScreenToWorld(entity.ElementCenter);
			bodyDef.Angle = (float)entity.ElementAngle;

			bodyParameters.SetParameters(bodyDef);

			Body body = simulator.World.CreateBody(bodyDef);
			if (body == null)
				return null;

			bodyParameters.SetParameters(body);


			ShapeHelper.CreateShapesFromElement(simulator, body, bodyParameters, entity);

			if (!bodyParameters.IsStatic)
			{
				body.SetMassFromShapes();
			}
			return body;
		}
	}
}
